/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.service.impl;

import org.springblade.common.enums.library.QuestionType;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.material.entity.PaperEntity;
import org.springblade.modules.material.vo.GetPaperDetailVO;
import org.springblade.modules.material.vo.GetPaperVO;
import org.springblade.modules.material.vo.PaperReleaseDetailVO;
import org.springblade.modules.material.vo.PaperVO;
import org.springblade.modules.material.mapper.PaperMapper;
import org.springblade.modules.material.service.IPaperService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 试卷表 服务实现类
 *
 * @author
 * @since 2023-03-22
 */
@Service
public class PaperServiceImpl extends BaseServiceImpl<PaperMapper, PaperEntity> implements IPaperService {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param paper
	 * @return
	 */
	@Override
	public IPage<PaperVO> selectPaperPage(IPage<PaperVO> page, PaperVO paper) {
		return page.setRecords(baseMapper.selectPaperPage(page, paper));
	}

	/**
	 * 获取试卷的所有内容
	 * @param paper
	 * @return
	 */
	@Override
	public GetPaperVO getPaper(PaperEntity paper) {
		return baseMapper.getPaper(paper);
	}

	@Override
	public List<GetPaperDetailVO> sortPaperDetailByQuestionType(List<GetPaperDetailVO> list) {
		Map<Integer, List<GetPaperDetailVO>> collectMap =list.stream().collect(Collectors.groupingBy(GetPaperDetailVO::getQuestionType));
		List<GetPaperDetailVO> sortList=new ArrayList<>();
		for(QuestionType questionType:QuestionType.values()){
			//获取对应题目类型的数据
			List<GetPaperDetailVO> questionTypeList = collectMap.get(questionType.getCode());

			if (CollectionUtil.isNotEmpty(questionTypeList)) {
				//按照时间升序排序
				List<GetPaperDetailVO> dataList=questionTypeList.stream().sorted(Comparator.comparing(GetPaperDetailVO::getCreateTime)).collect(Collectors.toList());
				if (CollectionUtil.isNotEmpty(dataList)) sortList.addAll(dataList);
			}

		}
		return sortList;
	}

	@Override
	public List<PaperReleaseDetailVO> sortExamPaperDetailByQuestionType(List<PaperReleaseDetailVO> list) {
		Map<Integer, List<PaperReleaseDetailVO>> collectMap =list.stream().collect(Collectors.groupingBy(PaperReleaseDetailVO::getQuestionType));
		List<PaperReleaseDetailVO> sortList=new ArrayList<>();
		for(QuestionType questionType:QuestionType.values()){
			//获取对应题目类型的数据
			List<PaperReleaseDetailVO> questionTypeList = collectMap.get(questionType.getCode());
			if (CollectionUtil.isNotEmpty(questionTypeList)) {
				Collections.shuffle(questionTypeList);
				questionTypeList.forEach(po->{
					if (CollectionUtil.isNotEmpty(po.getQuestionOptionList())) Collections.shuffle(po.getQuestionOptionList());
				});
				sortList.addAll(questionTypeList);
			}

		}
		return sortList;
	}

	@Override
	public List<PaperReleaseDetailVO> dealQuestionByQUestionType(List<PaperReleaseDetailVO> list) {
		Map<Integer, List<PaperReleaseDetailVO>> collectMap =list.stream().collect(Collectors.groupingBy(PaperReleaseDetailVO::getQuestionType));
		List<PaperReleaseDetailVO> sortList=new ArrayList<>();
		for(QuestionType questionType:QuestionType.values()){
			//获取对应题目类型的数据
			List<PaperReleaseDetailVO> questionTypeList = collectMap.get(questionType.getCode());
			if (CollectionUtil.isNotEmpty(questionTypeList)) {
				sortList.addAll(questionTypeList);
			}

		}
		return sortList;
	}

	@Override
	public boolean paperRestore(Long id) {
		return baseMapper.paperRestore(id);
	}


}
